<?php
namespace app\system\controller;

use app\common\model\ShopGoodsSkuModel;
use app\common\model\ShopGroupbuyModel;
use think\Request;

class ShopGroupbuyController extends BaseController
{
    private $groupbuyModel;
    private $goodsSkuModel;

    /**
     * 初始化
     */
    public function __construct(Request $request)
    {
        parent::__construct($request);
        $this->groupbuyModel = new ShopGroupbuyModel();
        $this->goodsSkuModel = new ShopGoodsSkuModel();
    }

    /**
     * 首页
     */
    public function index()
    {
        $map       = [];
        $query     = [];
        $searchKey = $this->request->param('searchKey');
        $searchName = $this->request->param('searchName');
        if(!empty($searchName)){
            $map[$searchKey] = ['like','%'.$searchName.'%'];
            $query ['searchKey'] = $searchKey;
            $query ['searchName'] = $searchName;
        }
        $status = $this->request->param('status',2);
        $query['status'] = $status;
        $map['status'] = $status;
        $list = $this->groupbuyModel->where($map)->paginate(10, false, [
            'query' => $query,
        ]);
        $this->assign('list', $list);
        $this->assign('query', $query);
        return $this->fetch();
    }
    /**
     * 是否推荐
     */
    public function commend(){
        $groupbuyId = $this->request->param('groupbuy_id');
        $isRecommend = $this->request->param('is_recommend');
        $isRecommend = abs($isRecommend-1);
        $result = $this->groupbuyModel->where('groupbuy_id',$groupbuyId)->setField('is_recommend',$isRecommend);
        if($result){
            $msg = '修改成功';
        }else{
            $msg = $this->groupbuyModel->getError();
        }
        $this->success($msg);
    }


    /**
     * 删除
     */
    public function del()
    {
        if ($this->request->isAjax()) {
            $groupbuyId = $this->request->param('groupbuy_id/d');
            $info = $this->groupbuyModel->where('groupbuy_id',$groupbuyId)->find();
            if($info['status'] == 2){
                $this->error('正在进行抢购,不能删除');
            }
            $this->groupbuyModel->delData($groupbuyId);
            $this->goodsSkuModel->where('sku_id',$info['status'])->setField(['promotion_type'=>0,'promotion_price'=>'0']);
            $this->success('删除成功！');
        }
    }


}
